package com.myrabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class DirectProductor {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("129.211.131.113");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        try {
            Connection connection = connectionFactory.newConnection();

            Channel channel = connection.createChannel();

            String exchangeName = "test_direct_exchange";
            String routingKey = "test.direct";

            for (int i = 0;i<10;i++) {
                //发送数据
                String msg = "Hello RabbitMQ direct exchange";
                channel.basicPublish(exchangeName,routingKey,null,msg.getBytes());
            }


            //关闭连接
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
